import { Col, Row, Switch } from 'antd'
import React, { useContext, useState } from 'react'
import { ProFormText } from '@ant-design/pro-form';
import styles from './index.less'
import { InputValue } from '../../SettingModal';
import { getWarnUpdate } from '@/services/warning';
import { history } from 'umi'
interface ChessItemProps {
  item: any,
}
export default function ChessItem(props: ChessItemProps) {
  const ctx1 = useContext(InputValue);
  const { item } = props;
  const { id: id1 } = history.location.query ?? {}
  const [va, setValue] = useState<number>()
  const [va1, setValue1] = useState<number>()
  const [va2, setValue2] = useState<number>()
  const [va3, setValue3] = useState<number>()

  const { i, ii, iii, iiii, name, nextName, nextpName, nextppName, value, value1, value2, value3, compareType, iId, isEnable, rule, comRule, comtimeUnit, timeUnit, gradeName } = item
  const onChange1 = async (checked: any) => {
    await getWarnUpdate({ activityId: id1, id: iId, isEnable: checked == true ? "1" : '2' });
  }
  const handle = (e: any, id?: string, key?: string,) => {
    // if (e?.target.value <= 100) {
    //   count = e?.target.value
    // } else if (e?.target.value > 100) {
    //   count = 100;
    // } else {
    const count = e?.target.value.replace(/[^\d.]/g, '')
    // }
    ctx1.changeValue(id, key, count)

    return count
  }
  const handle1 = (e: any, id?: string, key?: string, Unit?: string) => {

    // if (e?.target.value > 30 && Unit == "2") {
    //   count = 30
    // } else if (e?.target.value > 10 && Unit == "3") {
    //   count = 10
    // } else if (e?.target.value > 168 && Unit == "1") {
    //   count = 168
    // } else if (e?.target.value && e?.target.value < 1) {
    //   count = 1
    // } else {
    const count = e?.target.value.replace(/[^\d.]/g, '')
    // }

    ctx1.changeValue(id, key, count)
    return count
  }
  const onChange = (e: any, id?: string, key?: string, type?: number, rule1?: string, Unit?: string) => {
    console.log(Unit);
    if (type == 1 && Number(rule1) == 17) {
      ctx1.changeValue(id, key, e?.target?.value)
      setValue(e?.target?.value)
    }
    else if (type == 1) {
      setValue(handle(e, id, key))
    } else if (type == 2) {
      setValue1(handle(e, id, key))
    } else if (type == 3) {
      setValue2(handle1(e, id, key, Unit))
    } else if (type == 4) {
      setValue3(handle1(e, id, key, Unit))
    } else {
      setValue(handle1(e, id, key))
    }

  }

  return (

    <Row className={styles.chess_item} style={{ flexWrap: 'nowrap' }}>
      <Col span={10} className={styles.border_style}>
        {
          name && name !== '' ?
            <div className={styles.form_item}>
              {value1 == 0 ? '距离上次对弈' : ''}
              {
                timeUnit != 0 ? <ProFormText
                  rules={[
                    timeUnit == 2 ? {
                      validator: async (_, val) => {
                        val = val.replace(/[^\d.]/g, '')
                        if (val < 1 || val > 30) {
                          return Promise.reject(new Error('请输入1-30之间的正整数'));
                        } else if (!(/(^[1-9]\d*$)/.test(val))) {
                          return Promise.reject(new Error('请输入1-30之间的正整数'));
                        } else {
                          return;
                        }
                      }
                    } : {
                      validator: async (_, val) => {
                        val = val.replace(/[^\d.]/g, '')

                        if (val < 1 || val > 168) {
                          return Promise.reject(new Error('请输入1-168之间的正整数'));
                        } else if (!(/(^[1-9]\d*$)/.test(val))) {
                          return Promise.reject(new Error('请输入1-168之间的正整数'));
                        } else {
                          return;
                        }
                      }
                    }
                  ]}
                  name={i} fieldProps={{ onChange: (e) => onChange(e, i, 'timeLimit', 3, '', timeUnit), value: va2 }} initialValue={value} /> : null
              }
              <div style={{ maxWidth: "160px", display: 'flex', justifyContent: 'flex-end' }}>{name}</div>
              {
                value1 == 0 || ii == null ? null :
                  <ProFormText rules={[
                    rule == 4 && compareType == 1 || rule == 11 && timeUnit == 2 && compareType == 1 ? {
                      validator: async (_, val) => {
                        val = val.replace(/[^\d.]/g, '')
                        if (val < 1 || val > 300) {
                          if (rule == 4 && compareType == 1) {
                            return Promise.reject(new Error('请输入1-300之间的正整数'));
                          }
                        } else if (!(/(^[1-9]\d*$)/.test(val))) {
                          if (rule == 4 && compareType == 1) {
                            return Promise.reject(new Error('请输入1-300之间的正整数'));
                          }
                        }
                        return;
                      }
                    } : rule == 16 ? {
                      validator: async (_, val) => {
                        val = val.replace(/[^\d.]/g, '')

                        if (val < 100 || val > 1000) {
                          return Promise.reject(new Error('请输入100-1000之间的正整数'));
                        } else if (!(/(^[1-9]\d*$)/.test(val))) {
                          return Promise.reject(new Error('请输入100-1000之间的正整数'));
                        } else {
                          return;
                        }
                      }
                    } : {
                      validator: async (_, val) => {
                        val = val.replace(/[^\d.]/g, '')
                        if (val < 1 || val > 100) {
                          return Promise.reject(new Error('请输入1-100之间的正整数'));
                        } else if (!(/(^[1-9]\d*$)/.test(val))) {
                          return Promise.reject(new Error('请输入1-100之间的正整数'));
                        } else {
                          return;
                        }
                      }
                    }
                  ]} name={ii} fieldProps={{
                    onChange: (e) => onChange(e, i, 'value', 1, rule),
                    value: va,
                  }} initialValue={rule == 17 ? gradeName : value1} />
              }
              {
                value1 == 0 ? null :
                  <div>{nextName}</div>
              }
            </div> : null
        }
      </Col>
      <Col span={10} className={styles.border_style}>
        {
          nextppName !== '' ?
            <div className={styles.form_item}>
              {
                iii && iii !== '' ?
                  <ProFormText
                    rules={[
                      comtimeUnit == 2 || comtimeUnit == 3 ? {
                        validator: async (_, val) => {
                          const str = `请输入${comRule == 3 || comRule == 10 ? '1-10' : '1-30'}之间的正整数`
                          val = val.replace(/[^\d.]/g, '')
                          if (comRule == 3 || comRule == 10) {
                            if (val < 1 || val > 10) {
                              return Promise.reject(new Error(str));
                            } else if (!(/(^[1-9]\d*$)/.test(val))) {
                              return Promise.reject(new Error(str));
                            } else {
                              return;
                            }
                          } else {
                            if (val < 1 || val > 30) {
                              return Promise.reject(new Error(str));
                            } else if (!(/(^[1-9]\d*$)/.test(val))) {
                              return Promise.reject(new Error(str));
                            } else {
                              return;
                            }
                          }
                          return
                        }
                      } : {
                        validator: async (_, val) => {
                          val = val.replace(/[^\d.]/g, '')
                          if (val < 1 || val > 168) {
                            return Promise.reject(new Error('请输入1-168之间的正整数'));
                          } else if (!(/(^[1-9]\d*$)/.test(val))) {
                            return Promise.reject(new Error('请输入1-168之间的正整数'));
                          } else {
                            return;
                          }
                        }
                      }
                    ]}
                    name={iii} fieldProps={{
                      onChange: (e) => onChange(e, iii, 'timeLimit', 4, '', comtimeUnit), value: va3
                    }} initialValue={value2} /> : null
              }
              <span>{nextpName}</span>
              {
                iiii !== null ?
                  <ProFormText rules={[
                    comRule == 16 ? {
                      validator: async (_, val) => {
                        val = val.replace(/[^\d.]/g, '')

                        if (val < 100 || val > 1000) {
                          return Promise.reject(new Error('请输入100-1000之间的正整数'));
                        } else if (!(/(^[1-9]\d*$)/.test(val))) {
                          return Promise.reject(new Error('请输入100-1000之间的正整数'));
                        } else {
                          return;
                        }
                      }
                    } : comtimeUnit == 2 && comRule == 11 ? {
                      validator: async (_, val) => {
                        val = val.replace(/[^\d.]/g, '')

                        if (val < 1 || val > 300) {
                          return Promise.reject(new Error('请输入1-300之间的正整数'));
                        } else if (!(/(^[1-9]\d*$)/.test(val))) {
                          return Promise.reject(new Error('请输入1-300之间的正整数'));
                        } else {
                          return;
                        }
                      }
                    } : {
                      validator: async (_, val) => {
                        val = val.replace(/[^\d.]/g, '')
                        if (val < 1 || val > 100) {
                          return Promise.reject(new Error('请输入1-100之间的正整数'));
                        } else if (!(/(^[1-9]\d*$)/.test(val))) {
                          return Promise.reject(new Error('请输入1-100之间的正整数'));
                        } else {
                          return;
                        }
                      }
                    }
                  ]} name={iiii} fieldProps={{
                    onChange: (e) => onChange(e, iii, 'value', 2, comRule),
                    value: va1
                  }} initialValue={value3} /> : null
              }
              <span>{nextppName}</span>
            </div> : null
        }
      </Col>
      <Col span={4} style={{ textAlign: 'center' }} className={styles.border_style}>
        <Switch defaultChecked={isEnable == 1 ? true : false} onChange={onChange1} />

      </Col>
    </Row>

  )
}
